﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <vegas@ekameleon.net>.
  Portions created by the Initial Developer are Copyright (C) 2004-2007
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/
 
import andromeda.util.visitor.IVisitable;

import ewhiteboard.display.whiteboard.WhiteBoardDisplay;
import ewhiteboard.tools.whiteboard.IWhiteBoardTool;
import ewhiteboard.vo.ShapeVO;

import pegas.geom.Vector2;
import pegas.process.SimpleAction;

import vegas.errors.IllegalArgumentError;
import vegas.errors.TypeMismatchError;
import vegas.events.Delegate;
import vegas.events.EventListener;

/**
 * This class provides the freehand whiteboard tool.
 * @author eKameleon
 */
class ewhiteboard.tools.whiteboard.AbstractWhiteBoardTool extends SimpleAction implements IWhiteBoardTool
{

	/**
	 * Creates a new AbstractWhiteBoardTool instance.
	 * @param owner Optional reference of the WhiteBoardDisplay reference of this tool.
	 * @param bGlobal the flag to use a global event flow or a local event flow.
	 * @param sChannel the name of the global event flow if the {@code bGlobal} argument is {@code true}.
	 */	
	function AbstractWhiteBoardTool( owner:WhiteBoardDisplay ,  bGlobal:Boolean, sChannel:String ) 
	{
		super(bGlobal, sChannel) ;
		if ( owner != null )
		{
			setOwner( owner ) ;	
		}
		_listenerStart = new Delegate(this, this.start) ;
		_listenerStop  = new Delegate(this, this.stop) ;
		onMouseMove   = Delegate.create(this, move) ;
	}
	
	/**
	 * The canvas MovieClip reference of the whiteboard.
	 */
	public var canvas:MovieClip ;
	
	/**
	 * The layer MovieClip reference of the whiteboard.
	 */
	public var layer:MovieClip ;

	/**
	 * (read-only) Returns the shape value object of this tool.
	 */
	public function get shape():ShapeVO
	{
		return getShapeVO() ;	
	}

	/**
	 * Returns the owner WhiteBoardDisplay reference of this tool.
	 * @return the owner WhiteBoardDisplay reference of this tool.
	 */	
	public function getOwner():WhiteBoardDisplay
	{
		return _owner ;
	}

	/**
	 * Returns the shape value object of this tool. This method must be overrided.
	 * @return the shape value object of this tool.
	 */
	public function getShapeVO():ShapeVO
	{
		return new ShapeVO() ; // override this method
	}

	/**
	 * Returns the type of this tool. This method must be overrided.
	 * @return the type of this tool.
	 */
	public function getWhiteBoardType():Number
	{
		return null ; // override this method
	}

	/**
	 * Invoqued when the mouse move.
	 */
	public function move():Void 
	{
		// override this method.
	}

	/**
	 * Registers the whiteboard display of this tool.
	 */
	public function registerWhiteboard():Void
	{
		unregisterWhiteboard() ;
		_owner.addEventListener( _owner.getEventTypeCANVAS_PRESS()   , _listenerStart ) ;
		_owner.addEventListener( _owner.getEventTypeCANVAS_RELEASE() , _listenerStop  ) ;
	}

	/**
	 * Reset the process.
	 */
	public function reset():Void
	{
		// override this method.
	}

	/**
	 * Run the process.
	 */
	public function run():Void 
	{
		start() ; 
	}

	/**
	 * Sets the owner WhiteBoardDisplay reference of this tool.
	 */	
	public function setOwner( owner:WhiteBoardDisplay ):Void
	{
		unregisterWhiteboard() ;
		_owner = owner || null ;
		registerWhiteboard() ;
	}

	/**
	 * Start the process.
	 */
	public function start():Void
	{
		notifyStarted() ;
		_setRunning(true) ;
		reset() ;
		Mouse.addListener(this) ;
	}

	/**
	 * Stop the process.
	 */
	public function stop():Void
	{
		if ( running )
		{
			_setRunning(false) ;
			Mouse.removeListener(this) ;
			notifyFinished() ;
		}	
	}

	/**
	 * Unregisters the whiteboard display of this tool.
	 */
	public function unregisterWhiteboard():Void
	{
		if ( _owner != null )
		{
			_owner.removeEventListener( _owner.getEventTypeCANVAS_PRESS()   , _listenerStart ) ;
			_owner.removeEventListener( _owner.getEventTypeCANVAS_RELEASE() , _listenerStop ) ;
		}
	}
	
	/**
	 * Visit the IVisitable object.
	 * @throws IllegalArgumentError if the the IVisitable argument is 'null' or 'undefined'.
	 * @throws TypeMismatchErrror if the {@code IVisitable} passed-in parameter isn't a WhiteBoardDisplay instance.
	 */
	public function visit( o:IVisitable ):Void
	{
		if( o == null )
		{
			throw new IllegalArgumentError( this + " visit failed if the the IVisitable argument is 'null' or 'undefined'.") ; 	
		}
		if ( o instanceof WhiteBoardDisplay )
		{
			this.setOwner( WhiteBoardDisplay( o ) ) ;		
		}
		else
		{
			throw new TypeMismatchError( this + " visit failed if the IVisitable passed-in parameter isn't a WhiteBoardDisplay instance." ) ; 	
		}
	}
	
	/**	 
	 * The internal count of this process.
	 */
	private var _cpt:Number = 0 ;

	/**
	 * The event listener invoqued when the owner reference notify a canvas press event.
	 */
	private var _listenerStart:EventListener ;

	/**
	 * The event listener invoqued when the owner reference notify a canvas release event.
	 */
	private var _listenerStop:EventListener ;

	/**
	 * The memory array of this process.
	 */
	private var _memory:Array ;

	/**
	 * Invoqued when the mouse is moved.
	 */
	private var onMouseMove:Function ;
	
	/**
	 * The WhiteBoardDisplay reference of this visitor tool.
	 */
	private var _owner:WhiteBoardDisplay ;

	/**
	 * The internal vector value.
	 */
	private var _vector:Vector2 ;


}